// 暴力解法
function js_main1(nums, target) {
  for (let i = 0; i < nums.length - 1; i++) {
    for (let j = i + 1; j < nums.length; j++) {
      if (nums[i] + nums[j] === target) {
        return [i, j];
      }
    }
  }
}

// test
console.log(js_main1([2, 7, 11, 15], 9));
console.log(js_main1([3, 2, 4], 6));
console.log(js_main1([3, 3], 6));

// hash解法
function js_main2(nums, target) {
  const map = new Map();
  const len = nums.length;
  map.set(nums[0], 0);
  for (let i = 1; i < len; i++) {
    const num = nums[i];
    const key = target - num;
    if (map.has(key)) {
      return [map.get(key), i];
    }
    map.set(num, i);
  }
  return [];
};

// test
console.log(js_main2([2, 7, 11, 15], 9));
console.log(js_main2([3, 2, 4], 6));
console.log(js_main2([3, 3], 6));
